package timer;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimerTask;

/**
 * Created by Ming on 2017/6/3.
 */
public class MytTimerTask extends TimerTask {
    private String name;
    private Integer count = 0;

    public MytTimerTask(String inputName) {
        name = inputName;
    }


    @Override
    public void run() {
        if(count < 3){
            //以yyyy-MM-dd HH:mm:ss的格式打印当前执行时间
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(simpleDateFormat.format(calendar.getTime()));

            //打印当前name的内容
            System.out.println(name);
            count++;
        }else {
            cancel();
            System.out.println("cancel");
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
